package com.yml.mall.order;

import com.alibaba.fastjson.JSON;
import com.yml.mall.carts.pojo.CartsDO;
import com.yml.mall.currentexception.MyException;
import com.yml.mall.enums.OrderStatus;
import com.yml.mall.enums.PayMentType;
import com.yml.mall.enums.ResponseEnum;
import com.yml.mall.order.dao.OrderMapper;
import com.yml.mall.order.pojo.OrderVO;
import com.yml.mall.product.dao.ProductMapper;
import com.yml.mall.product.pojo.ProductDO;
import com.yml.mall.shipping.dao.ShippingMapper;
import com.yml.mall.shipping.pojo.ShippingDO;
import com.yml.mall.user.dao.UserMapper;
import com.yml.mall.utils.StringRedisUtil;
import com.yml.mall.vo.OrderItemVO;
import com.yml.mall.vo.ResponseVO;
import com.yml.mall.vo.ShippingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.yml.mall.consts.myConst.CART_PRO_FIX;

/**
 * @ClassName OrderServiceImp
 * @Author Administrator
 * @Date 2020/4/15   17:24
 */
@Service
@Slf4j
public class OrderServiceImpl implements  OrderService{
    private final ShippingMapper shippingMapper;
    private final OrderMapper orderMapper;
    private final ProductMapper productMapper;
    private final UserMapper userMapper;
    @Autowired
    StringRedisUtil<CartsDO> stringRedisUtil;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    OrderServiceImpl(ShippingMapper shippingMapper,OrderMapper orderMapper,
                     ProductMapper productMapper,UserMapper userMapper,
                     RedisTemplate<String , String> redisTemplate){
        this.shippingMapper=shippingMapper;
        this.orderMapper=orderMapper;
        this.productMapper=productMapper;
        this.userMapper=userMapper;
    }


    @Transactional
    @Override
    public ResponseVO<OrderVO> create(Integer userId, Integer shippingId) {
        ShippingDO shippingDO=shippingMapper.selectByPrimaryKey(shippingId);
        if(!shippingDO.getUserId().equals(userId)) throw new RuntimeException();

        List<OrderItemVO> orderItemVOList = getSelectedCartsVO(userId);
        if(orderItemVOList == null || orderItemVOList.size()==0)
            //TODO
            throw new RuntimeException();
        BigDecimal payment =BigDecimal.valueOf(0);

        Long orderNo = orderItemVOList.get(0).getOrderNo();

        for (OrderItemVO orderItemVO:orderItemVOList ){
            payment.add(orderItemVO.getTotalPrice());
        }

        ShippingVO shippingVO = new ShippingVO();
        BeanUtils.copyProperties(shippingDO,shippingVO);

        OrderVO orderVO =new OrderVO(orderNo,shippingId,payment, PayMentType.WX.getCode(),0, OrderStatus.NOT_PAY.getCode(),new Date()) ;

        orderVO.setShippingVo(shippingVO);
        orderVO.setOrderItemVoList(orderItemVOList);


        return ResponseVO.success(orderVO);
    }


    public List<OrderItemVO> getSelectedCartsVO(Integer userId){
        String redisKey = String.format(CART_PRO_FIX,userId);

        List<String> values= stringRedisUtil.getAllValueHashRedis(redisKey);
        log.info(values.toString());
        List<Integer> ids=new ArrayList<>();
        List<ProductDO> productDOList =new ArrayList<>();

        for(int i=0;i<values.size();i++) {
            CartsDO cartsDO = JSON.parseObject(values.get(i),CartsDO.class);
            if (values.get(i)== null) throw new MyException(ResponseEnum.ERROR);
            if(!cartsDO.isProductSelected())continue;
            ids.add(cartsDO.getProductId());

        }
        productDOList = productMapper.selectForEachId(ids);

        List<OrderItemVO> orderItemVOList = new ArrayList<>();
        Long orderNo = System.currentTimeMillis();
        for(int i=0;i<productDOList.size();i++){
            ProductDO productDO =productDOList.get(i);
            CartsDO cartsDO = JSON.parseObject(values.get(i),CartsDO.class);

            OrderItemVO orderItemVO = new OrderItemVO();
            orderItemVO.setOrderNo(orderNo);
            orderItemVO.setCreateTime(productDO.getCreateTime())
                    .setCurrentUnitPrice(productDO.getPrice())
                    .setProductId(productDO.getId())
                    .setProductImage(productDO.getMainImage())
                    .setProductName(productDO.getName())
                    .setQuantity(cartsDO.getQuantity())
                    .setTotalPrice(productDO.getPrice().add(BigDecimal.valueOf(cartsDO.getQuantity())));
            orderItemVOList.add(orderItemVO);
        }
        return  orderItemVOList;
    }



}
